//
//  SchoolListCell.m
//  edu
//
//  Created by baobaodp on 13-8-29.
//  Copyright (c) 2013年 baobaodp. All rights reserved.
//

#import "SchoolListCell.h"
#import "Record.h"
#import "DBbase.h"
#import "SchoolRecruitlInfoView.h"

@implementation SchoolListCell

@synthesize name;
@synthesize infoViews;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void) setData:(Record *) record index: (int) rowIndex
{
    self.m_record =record;
    self.name.text = [record getFieldValueByName:@"sch_Name"];
    
    UIImage *img=[UIImage imageNamed:[NSString stringWithFormat:@"tempSch%@",[record getFieldValueByName:@"sch_Id"]]];
    self.imgSch.image =img;
    
    //移除infoView
    for (SchoolRecruitlInfoView *oneView in self.contentView.subviews ) {
        if ([oneView isKindOfClass:[SchoolRecruitlInfoView class]]) {
            [oneView removeFromSuperview];
        }
    }
    
    NSMutableArray* datas = [self getRecords];
    
    NSInteger index = 0;
    for (Record* data in datas) {
        
        SchoolRecruitlInfoView *infoView = [self getInfoView :index];
        [infoView setData:data];
        infoView.frame = CGRectMake(10, 160+(index*64), infoView.frame.size.width, 64);
        [self.contentView addSubview:infoView];
        
        index = index+1;
    }
    
    self.imgBody.frame = CGRectMake(self.imgBody.frame.origin.x, self.imgBody.frame.origin.y, self.imgBody.frame.size.width,self.imgBody.frame.size.height+(datas.count*64));
    
    self.imgDown.frame = CGRectMake(self.imgDown.frame.origin.x, 166+(datas.count*64), self.imgDown.frame.size.width,self.imgDown.frame.size.height);
}

-(NSMutableArray* )getRecords
{
    NSString *sch_Id = [self.m_record getFieldValueByName:@"sch_Id"] ;
    
    NSString *querySql = [NSString stringWithFormat:@"select T1.* from T_Souce T1 where T1.sch_Id = %@ order by T1.S_Ym " ,sch_Id];
    
    const char *sql = [querySql UTF8String];
    
    NSMutableArray* datas = getDbRecords(sql,@"T_Souce",[NSArray arrayWithObjects:@"soucd_Id", nil]);
    
    return datas;
    
}

-(SchoolRecruitlInfoView*) getInfoView :(NSInteger)i_index
{
    if(i_index < self.infoViews.count )
    {
        return [self.infoViews objectAtIndex:i_index];
    }
    
    NSArray *nibVew = [[NSBundle mainBundle] loadNibNamed:@"SchoolRecruitlInfoView" owner:nil options:nil];
    
    SchoolRecruitlInfoView *view = [nibVew objectAtIndex:0];
    
    [self.infoViews addObject:view];
    
    return view;
}

@end
